package site.kitcheng.springboottest.test;

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

/**
 * ClassName: RSADecryptWithSalt
 * Package: site.kitcheng.springboottest.test
 * Description:
 *
 * @Author jiangCheng
 * @Create 2025/2/24 11:13
 * @Version 1.0
 */
public class RSADecryptWithSalt {

    public static String decryptWithSalt(String encryptedDataWithSalt, PrivateKey privateKey) throws Exception {
        // 分离盐值和加密数据
        String[] parts = encryptedDataWithSalt.split(":");
        String salt = parts[0];
        String encryptedData = parts[1];

        // 使用RSA私钥解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));

        // 得到拼接的明文（盐值+原始数据）
        String decryptedString = new String(decryptedData);

        // 移除盐值，得到原始数据
        return decryptedString.substring(salt.length());
    }

    public static void main(String[] args) throws Exception {

        PrivateKey privateKeyFromBase64 = KeyConverter.getPrivateKeyFromBase64("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC7TKpI65Iujt1LtmW4UhX9KrQNgc+t3Zwm1sTl7SjJGHeXHMe+ubxyU4ouPNu0uEs62LAeXzvrFYktor+mbvaXon1oZrNH97QOy6hZid53m5sFjwETHaMinfyxSf3EZXzfJFH+CU+idacis+Ka+HTkPZTpPTZjuWuDt5dnIG2eUjwd1r0+1/cVTndUwcQwsTy9H9HuhOT96PtaAE1OBZyd72WSOvm7xCJxnhDHjGUTjbEhz1SqaDoRbnxsMzPqBOaNjRwEIG9bXVwIlk+JUH5z+2t9iHvUKXEUYyYr1vJPOCFk7BsLxd0lRCqWp0MjLDOoFngqdppRw05J85Ae+6ttAgMBAAECggEAIfHnjLaw9CItRisE7W5nEM57+lSz9GFNCxbycW9VbHi0N91d8BmOZzPvv6iE8xBRhhU+egqte9IX3f74d+v1Yg7vZovyYxEn/2R2mt+rQ4cehdfgjHvbF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MDadBliiHtHyxGjVR7mTHY4+F07jRMERX6t");
        PublicKey publicKeyFromBase64 = KeyConverter.getPublicKeyFromBase64("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu0yqSOuSLo7dS7ZluFIV/Sq0DYHPrd2cJtbE5e0oyRh3lxzHvrm8clOKLjzbtLhLOtiwHl876xWJLaK/pm72l6J9aGazR/e0DsuoWYned5ubBY8BEx2jIp38sUn9xGV83yRR/glPonWnIrPimvh05D2U6T02Y7lrg7eXZyBtnlI8Hda9Ptf3FU53VMHEMLE8vR/R7oTk/ej7WgBNTgWcne9lkjr5u8QicZ4Qx4xlE42xIc9Uqmg6EW58bDMz6gTmjY0cBCBvW11cCJZPiVB+c/trfYh71ClxFGMmK9byTzghZOwbC8XdJUQqlqdDIywzqBZ4KnaaUcNOSfOQHvurbQIDAQAB");
        // 生成RSA密钥对

        KeyPair keyPair = KeyPairGeneratorUtil.generateKeyPair();
        String s = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
        PrivateKey privateKey = keyPair.getPrivate();
        String s2 = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());

        // 假设我们已加密的数据（盐值+密文）
        String encryptedDataWithSalt = "xKYSdeLgqIf+aZLJZN4HsQ==:XZNp7rSnlWUa6wpuBCBaP/GufHarAkl31se7ViBYTRlcIB5RGUNn61T+X6yJIeBgv+irBhiCaxqAD5byr5ZyDszfGkaqgFP7FsKYcR/p5MH47NQkGf5PJQ4vDrmJE9s6nzEuKu1cLMnkZjUwJbyRN1leB9AJD4ZJ002kIHsPvKXgVhWs3Kma3uM7uf1D4a8SBQ5WtlVFb2wjPxBJJS4/g2IwTypoKffe0LuZ1glAiItUPltvTFLYC/K17K6xivjg0GGNAOepwAQMuXt0C/Le+tBgTPgoRk3o+GE37zWInHd1BkQye6U4vpM/v8PoR/bTy/pl9Yko9HJc7KpA2GjtHA==";

        // 使用私钥解密
        String decryptedData = decryptWithSalt(encryptedDataWithSalt, privateKeyFromBase64);
        System.out.println("Decrypted Data: " + decryptedData);
    }
}

